package klk.shop.z.kev;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;

import klk.shop.objects.Address;
import klk.shop.objects.BankAccount;
import klk.shop.objects.Customer;
import klk.shop.utilities.HTMLUtilities;
import klk.shop.utilities.MysqlUtilities;

/**
 * @author kev
 * Diese Klasse erweitert die Customer-Klasse um den
 * -Firmennamen (companyName) und der
 * -Steuernummer (companyTaxNumber)
 */
public class BusinessCustomer extends Customer {
	
	
	protected String companyName;
	protected String companyTaxNumber;

	
	public String getCompanyName() {
		return companyName;
	}
	public void setCompanyName(String companyName) {
		this.companyName = companyName;
	}
	
	public String getCompanyTaxNumber() {
		return companyTaxNumber;
	}
	public void setCompanyTaxNumber(String companyTaxNumber) {
		this.companyTaxNumber = companyTaxNumber;
	}
	
	
	public BusinessCustomer() {
		this(-1, "", 0, "", "", new Date(), new Date(System.currentTimeMillis()), "de", false,
				new ArrayList<Address>(),
				new ArrayList<BankAccount>(),
				"", "");
	}
	public BusinessCustomer(int customerId, String email, int title,
			String name, String surname, Date birthDate, Date registrationDate,
			String language, boolean isVerified, 
			ArrayList<Address> addressList,
			ArrayList<BankAccount> bankAccountList,
			String companyName, String companyTaxNumber) {
		super(customerId, email, title, name, surname, birthDate, registrationDate, language, 
				isVerified, addressList, bankAccountList);
		this.companyName = companyName;
		this.companyTaxNumber = companyTaxNumber;
	}
	public BusinessCustomer(Customer customer) {
		this(	
			customer.getCustomerId(), 
			customer.getEmail(),
			customer.getTitle(),
			customer.getName(),
			customer.getSurname(),
			customer.getBirthDate(),
			customer.getRegistrationDate(),
			customer.getLanguage(),
			customer.isVerified(),
			new ArrayList<Address>(),
			new ArrayList<BankAccount>(),
			"","" 
		);		
	}

	
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder(super.toString() + "BusinessCustomer:" + nl);
		sb.append("companyName:      " + companyName + nl);
		sb.append("companyTaxNumber: " + companyTaxNumber + nl);
		return sb.toString();
	}
	
	@Override
	public String toHtml() {
		StringBuilder sb = new StringBuilder( super.toString() );
		sb.append( "<table>" );
		sb.append( nl );
		sb.append( HTMLUtilities.TableRow("BusinessCustomer:") );
		sb.append( HTMLUtilities.TableRow("companyName:", companyName) );
		sb.append( HTMLUtilities.TableRow("companyTaxNumber:", companyTaxNumber) );
		sb.append( "</table>" );
		sb.append( nl );
		return sb.toString();
	}
	
	public static BusinessCustomer getBusinessCustomerFromDatabase(Customer customer) {
		BusinessCustomer businessCustomer = new BusinessCustomer(customer);
		
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
				
		try	{
			
			conn= MysqlUtilities.getConnection();
			stmt = conn.createStatement();
			
			rs = stmt.executeQuery(
				"SELECT " +
					"company_name, " +
					"company_tax_number " + 
				"FROM " +
					"business_customers " +
				"WHERE customer_id="+customer.getCustomerId()+"; "
			);
			
			rs.next();
			
			businessCustomer.setCompanyName(rs.getString("company_name"));
			businessCustomer.setCompanyTaxNumber(rs.getString("company_tax_number"));
						
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (rs != null) try { rs.close(); } catch(Exception e) {}
			if (stmt != null) try { stmt.close(); } catch(Exception e) {}
			if (conn != null) try { conn.close(); } catch(Exception e) {}
	    }
		
		return businessCustomer;	
	}
}
